#include "Missle.h"
#include "Level.h"
#include "GamePlayState.h"
#include "Player.h"
#include "Particle.h"
#include "Game.h"
#include "DestroyEntityMessage.h"

Missle::Missle(Enemy* tmp)
{
	type = OBJ_BULLET;
	target = nullptr;
	hasTarget = false;
	delayTimer = 0;
	delay = 1.0f;
	x = 0;
	y = 0;

	if(tmp != nullptr)
	{
		hasTarget = true;
		target = tmp;
		target->AddRef();
	}

	rotation = 0;

	trail = Emitter::LoadEmitter("emitters/missle_trail.xml");
}

Missle::~Missle(void)
{
	if( hasTarget == true )
		target->Release();

	trail->Loop(false);
	trail->Shutdown();
	delete trail;
}

void Missle::Update(float elapsed)
{
	delayTimer += elapsed;
	
	if( hasTarget == true && target->GetHealth() <= 0)
	{
		hasTarget = false;
		target->Release();
		target = nullptr;
	}

	if(delayTimer < delay)
	{
		switch(delayID)
		{
		case 0:
			{
				x = 0;
				y = (GetMoveSpeed() * elapsed);
			}
			break;

		case 1:
			{
				x = 0;
				y = -(GetMoveSpeed() * elapsed);
			}
			break;

		case 2:
			{
				x = -(GetMoveSpeed() * elapsed);
				y = -(GetMoveSpeed() * elapsed);
			}
			break;

		case 3:
			{
				x = -(GetMoveSpeed() * elapsed);
				y = (GetMoveSpeed() * elapsed);
			}
			break;

		case 4:
			{
				x = -(GetMoveSpeed() * elapsed);
				y = 0;
			}
			break;

		case 5:
			{
				x = (GetMoveSpeed() * elapsed);
				y = (GetMoveSpeed() * elapsed);
			}
			break;

		case 6:
			{
				x = (GetMoveSpeed() * elapsed);
				y = 0;
			}
			break;

		case 7:
			{
				x = (GetMoveSpeed() * elapsed);
				y = -(GetMoveSpeed() * elapsed);
			}
			break;
		}
	}
	else if(hasTarget == true)
	{
		if(GetX() == target->GetX() + target->GetWidth()/2)
			x = 0;
		else if(GetX() > target->GetX() + target->GetWidth()/2)
			x = -(GetMoveSpeed() * elapsed);
		else
			x = (GetMoveSpeed() * elapsed);

		if(GetY() == target->GetY() + target->GetHeight()/2)
			y = 0;
		else if(GetY() > target->GetY() + target->GetHeight()/2)
			y = -(GetMoveSpeed() * elapsed);
		else
			y = (GetMoveSpeed() * elapsed);
	}
	// Bug Fix #32 Start - Brandon Jones
	else
	{
		if(x < 0)
			x = -(GetMoveSpeed() * elapsed);
		else if(x > 0)
			x = (GetMoveSpeed() * elapsed);

		if(y < 0)
			y = -(GetMoveSpeed() * elapsed);
		else if(y > 0)
			y = (GetMoveSpeed() * elapsed);

		if(x == 0 && y == 0)
			x = (GetMoveSpeed() * elapsed);
	}
	// Bug Fix #32 End

	SetPosition((int)(GetX() + x), (int)(GetY() + y));

	//Top
	if(x == 0 && y > 0)
		SetRotation(2);
	//Top-Right
	if(x > 0 && y > 0)
		SetRotation(0);
	//Right
	if(x > 0 && y == 0)
		SetRotation(0);
	//Bottom-Right
	if(x > 0 && y < 0)
		SetRotation(0);
	//Bottom
	if(x == 0 && y < 0)
		SetRotation(3);
	//Bottom-Left
	if(x < 0 && y < 0)
		SetRotation(1);
	//Left
	if(x < 0 && y == 0)
		SetRotation(1);
	//Top-Left
	if(x < 0 && y > 0)
		SetRotation(1);

	int width = Game::GetInstance()->GetScreenWidth();
	int height = Game::GetInstance()->GetScreenHeight();

	int camX = GamePlayState::GetInstance()->GetCamX();
	int camY = GamePlayState::GetInstance()->GetCamY();

	if(GetX() + GetWidth() > camX + width)
	{
		DestroyEntityMessage* msg = new DestroyEntityMessage(this);
		MessageSystem::GetInstance()->SendMsg(msg);
		msg = nullptr;
	}
	if(GetX() < camX)
	{
		DestroyEntityMessage* msg = new DestroyEntityMessage(this);
		MessageSystem::GetInstance()->SendMsg(msg);
		msg = nullptr;
	}
	if(GetY() + GetHeight() > camY + height)
	{
		DestroyEntityMessage* msg = new DestroyEntityMessage(this);
		MessageSystem::GetInstance()->SendMsg(msg);
		msg = nullptr;
	}	
	if(GetY() < camY)
	{
		DestroyEntityMessage* msg = new DestroyEntityMessage(this);
		MessageSystem::GetInstance()->SendMsg(msg);
		msg = nullptr;
	}

	//trail->SetPosition(GetX(), GetY() + GetHeight()/2);
	trail->Update(elapsed);

}

void Missle::Render()
{
	CSGD_Direct3D::GetInstance()->GetSprite()->Flush();

	trail->Render();

	if(GetTextureID() != -1)
	{
		CSGD_TextureManager::GetInstance()->Draw(GetTextureID(), int(GetX()- GamePlayState::GetInstance()->GetCamX()),
			int(GetY() - GamePlayState::GetInstance()->GetCamY()), 1.5f, 1.5f, nullptr, GetWidth()/2.0f, GetHeight()/2.0f, rotation);
	}
}

void Missle::SetDirection(int d)
{
	if(GetOwner())
	{
		int width = GetOwner()->GetWidth();
		int height = GetOwner()->GetHeight();
		int x = (int)GetOwner()->GetX();
		int y = (int)GetOwner()->GetY();

		/*if(d % 2 == 0)
			--d;*/

		Projectile::SetDirection(d);

		//float dir = 0.0f;

		switch(d)
		{
			//Up
		case 3:
			//dir = SGD_PI;
			SetPosition(x, y);
			break;
			//Right
		case 0:
			//dir = (3*SGD_PI)/2;
			SetPosition(x + width, y + height);
			break;
			//Down
		case 2:
			//dir = 0;
			SetPosition(x, y + height);
			break;
			//Left
		case 1:
			//dir = (SGD_PI)/2;
			SetPosition(x - width, y + height);
			break;
		}

		trail->SetPosition(GetX(), GetY());

		//trail->GetMaster()->SetRotation(dir);

		trail->Loop();
	}
}

void Missle::SetRotation(int r)
{
	/*if(r % 2 == 0)
	--r;
	*/
	switch(r)
	{
		//Up
	case 3:
		{
			rotation = (3*SGD_PI)/2;
			trail->SetPosition(GetX() + 12, GetY() + 16);
		}
		break;
		//Right
	case 0:
		{
			rotation = 0;
			trail->SetPosition(GetX(), GetY() + (3*GetHeight())/4);
		}
		break;
		//Down
	case 2:
		{
			rotation = (SGD_PI)/2;
			trail->SetPosition(GetX() + 12, GetY());
		}
		break;
		//Left
	case 1:
		{
			rotation = (SGD_PI);
			trail->SetPosition(GetX() + GetWidth(), GetY() + (3*GetHeight())/4);
		}
		break;
	}
}
